package com.appspot.codelab.docs.client.content.dialogs;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.RunAsyncCallback;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * A dialog window containing a form for uploading files.
 */
public class DynamicUploadDialog extends DynamicFormDialog {

  /**
   * Contains form fields for uploading files.
   */
  protected class UploadFormContents extends FormContents {
	
    /**
     * Builds the file upload form.
     */
	@Override
	protected void buildForm() {
	  content.setWidth("600px");
      submit.addClickHandler(new ClickHandler() {
        public void onClick(ClickEvent event) {
        }
      });
      cancel.addClickHandler(new ClickHandler(){
          public void onClick(ClickEvent event) {
            hide();
          }
      });
	}
  
    /**
     * Resets the form fields to their default values.
     */
    @Override
    public void resetForm() {
    }

  }

  protected static DynamicUploadDialog instance;
  
  /**
   * Retrieves the single instance of this class, using asynchronous instantiation.
   */
  public static DynamicUploadDialog get() {
    if (instance == null) {
      instance = new DynamicUploadDialog();
    }
    return instance;
  }
  
  /**
   * Constructs a dialog window containing a form for selecting a project.
   */
  public DynamicUploadDialog() {
    super("Upload Files", true, "600px", null);
  }

  /**
   * Retrieves the dialog's contents asynchronously.
   * 
   * @param callback the callback carrying the content widget
   */
  @Override
  protected void getFormContents(final AsyncCallback<FormContents> callback) {
    GWT.runAsync(new RunAsyncCallback() {
		@Override
		public void onFailure(Throwable reason) {
		  callback.onFailure(reason);
		}
		@Override
		public void onSuccess() {
	      UploadFormContents form = new UploadFormContents();
	      form.resetForm();
		  callback.onSuccess(form);
		}
    });
  }

}
